//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents a custom query that consists of key value pairs. 
	/// </summary>
	public class CustomQueryNode : QueryBaseNode
	{
		#region Fields
		private List<EditableKeyValue> editableAttributes = new List<EditableKeyValue>();
		private string typeName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the attributes for the custom query.
		/// </summary>
		/// <value>
		/// The attributes for the custom query.
		/// </value>		
		[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
		[CustomAttributesValidation]
		[QueryCategory("CategoryProvider")]
		[QueryDescription("CustomQueryExtensionsDescription")]
		public List<EditableKeyValue> Attributes
		{
			get
			{
				return editableAttributes;
			}
		}

		/// <summary>
		/// Gets or sets the <see cref="Type"/> that implements <see cref="IQuery"/>.
		/// </summary>
		/// <value>
		/// The <see cref="Type"/> that implements <see cref="IQuery"/>.
		/// </value>
		[Required]
		[Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(IQuery), typeof(CustomQueryData))]
		[QueryCategory("CategoryProvider")]
		[QueryDescription("CustomQueryNodeTypeDescription")]
		public string TypeName
		{
			get { return typeName; }
			set { typeName = value; }
		}

		/// <summary>
		/// Gets a <see cref="CustomQueryData"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="CustomQueryData"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override QueryDataBase QueryData
		{
			get
			{
				CustomQueryData customData = new CustomQueryData(Name, typeName);
				customData.Attributes.Clear();
				foreach (EditableKeyValue keyValue in editableAttributes)
				{
					customData.Attributes.Add(keyValue.Key, keyValue.Value);
				}
				return customData;
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="CustomQueryNode"/> class.
		/// </summary>
		public CustomQueryNode()
			: this(new CustomQueryData(InternalResources.DefaultCustomQueryNodeName, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="CustomQueryNode"/> class with a <see cref="CustomQueryData"/> object.
		/// </summary>
		/// <param name="customQueryData">The <see cref="CustomQueryData"/> to display.</param>
		public CustomQueryNode(CustomQueryData customQueryData)
		{
			if (null == customQueryData) throw new ArgumentNullException("customQueryData");

			Rename(customQueryData.Name);
			foreach (string key in customQueryData.Attributes)
			{
				editableAttributes.Add(new EditableKeyValue(key, customQueryData.Attributes[key]));
			}
			this.typeName = customQueryData.TypeName;
		}
		#endregion
	}
}